import { getWsUrl } from './domain'

function Ws () {
  if (!('WebSocket' in window)) {
    console.warn('browser not support websocket!!!')
    return
  }

  const touch = () => {
    // this.touchTimer = setTimeout(() => {
    //   this.sendData({ fid: '001', user: 'touch' })
    // }, 10 * 1000)
  }

  const reconnect = () => {
    this.ws = null
    if (this.touchTimer) {
      clearTimeout(this.touchTimer)
    }
    if (this.reconnectTimer) {
      clearTimeout(this.reconnectTimer)
    }
    this.reconnectTimer = setTimeout(() => {
      connect()
    }, 5000)
  }
  const connect = () => {
    const wsUrl = getWsUrl()
    this.ws = new WebSocket(wsUrl)
    this.readyState = WebSocket.CONNECTING
    this.ws.onopen = (e) => {
      this.readyState = WebSocket.OPEN
      touch()
      if (typeof this.openReceive === 'function') {
        this.openReceive(e)
      }
    }
    this.ws.onclose = (e) => {
      this.readyState = WebSocket.CLOSED
      if (typeof this.closeReceive === 'function') {
        this.closeReceive(e)
      }
      if (!this.forceClose) {
        reconnect()
      }
    }
    this.ws.onmessage = (e) => {
      try {
        const data = JSON.parse(e.data)
        if (data.fid === '002') {
          touch()
        } else {
          this.messageReceive(data)
        }
      } catch (e) {
      }
    }
    this.ws.onerror = (e) => {
      this.readyState = WebSocket.CLOSED
      if (typeof this.errorReceive === 'function') {
        this.errorReceive(e)
      }
      reconnect()
    }
  }
  connect()
}

Ws.prototype.onOpen = function (func) {
  this.openReceive = func
}

Ws.prototype.onMessage = function (func) {
  this.messageReceive = func
}

Ws.prototype.onClose = function (func) {
  this.closeReceive = func
}

Ws.prototype.onError = function (func) {
  this.errorReceive = func
}

Ws.prototype.disconnect = function () {
  if (this.ws) {
    this.readyState = WebSocket.CLOSING
    this.forceClose = true
    this.ws.close(-1, 'close')
  }
}

Ws.prototype.sendData = function (data = {}) {
  if (this.ws && this.readyState === WebSocket.OPEN) {
    this.ws.send(JSON.stringify(data))
  }
}

const createWs = () => {
  return new Ws()
}

export default createWs
